package com.xie.leetcode.string;

//539. 最小时间差
//        给定一个 24 小时制（小时:分钟 "HH:MM"）的时间列表，找出列表中任意两个时间的最小时间差并以分钟数表示。
//
//
//
//        示例 1：
//
//        输入：timePoints = ["23:59","00:00"]
//        输出：1
//        示例 2：
//
//        输入：timePoints = ["00:00","23:59","00:00"]
//        输出：0
//
//
//        提示：
//
//        2 <= timePoints <= 2 * 104
//        timePoints[i] 格式为 "HH:MM"

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author xiezhendong
 * @date 2022/1/18
 */
public class FindMinDifference {

    public static void main(String[] args) {
        FindMinDifference findMinDifference = new FindMinDifference();
        List<String> list = new ArrayList<>();
        list.add("23:59");
        list.add("00:00");
        System.out.println(findMinDifference.findMinDifference(list));
        list = new ArrayList();
        list.add("00:00");
        list.add("23:59");
        list.add("00:00");
        System.out.println(findMinDifference.findMinDifference(list));
    }

    public int findMinDifference(List<String> timePoints) {
        Collections.sort(timePoints);
        int ret = Integer.MAX_VALUE;
        int t0Minutes = getMinutes(timePoints.get(0));
        int preMinutes = t0Minutes;
        for (int i = 1; i < timePoints.size(); ++i) {
            int minutes = getMinutes(timePoints.get(i));
            ret = Math.min(ret, minutes - preMinutes);
            preMinutes = minutes;
        }
        ret = Math.min(ret, t0Minutes + 1440 - preMinutes);
        return ret;
    }

    public int getMinutes(String t) {
        return ((t.charAt(0) - '0') * 10 + (t.charAt(1) - '0')) * 60 + (t.charAt(3) - '0') * 10 + (t.charAt(4) - '0');
    }
}
